package com.miraclesea.module.rbac.controller;

import java.util.Collection;

import javax.annotation.Resource;
import javax.validation.groups.Default;

import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.miraclesea.framework.dao.exception.DataIntegrityViolationException;
import com.miraclesea.framework.group.New;
import com.miraclesea.module.rbac.entity.Role;
import com.miraclesea.module.rbac.service.RoleService;
import com.miraclesea.webmvc.controller.AsyncBaseMangementController;
import com.miraclesea.webmvc.controller.MvcCallback;
import com.miraclesea.webmvc.vo.BaseResponse;

@Controller
@RequestMapping("api/role")
public final class AsyncRoleController extends AsyncBaseMangementController<Role> {
	
	@Resource
	private RoleService roleService;
	
	@RequestMapping("list")
	@ResponseBody
	public Collection<Role> findAll() {
		return roleService.findAll(new PageRequest(0, 10)).getContent();
	}
	
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<BaseResponse<Role>> create(@RequestBody @Validated({ New.class, Default.class }) final Role role, final BindingResult result) {
		return add(role, result, new MvcCallback() {
			
			@Override
			public void execute() throws DataIntegrityViolationException {
				roleService.create(role);
			}
			
		});
	}
	
}
